/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProductPanel.java
 *
 * Created on 26-10-2011, 20:43:32
 */
package GUILager;
import ControlLager.*;
import ModelLager.*;
import javax.swing.JTable;

/**
 *
 * @author Jonas Kristensen
 */
public class ProductPanel extends javax.swing.JPanel {
    private ProductPCTR products;
    private SalesOrderPCTR salesOrder;
    private CustomerPCTR customers;    
    /** Creates new form ProductPanel */
    public ProductPanel() {
        initComponents();
        products = new ProductPCTR();
        salesOrder = new SalesOrderPCTR();
        customers = new CustomerPCTR();      
        displayLatestOrders();
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton5 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Flere resultater"));
        jPanel1.setPreferredSize(new java.awt.Dimension(383, 321));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "ProductID", "Name", "PurchasePrice", "SalesPrice", "RentPrice", "CountryOfOrigin", "Min Stock", "BarCode", "SupplierID", "Type", "Description", "Size", "Colour", "Fabric", "DescriptionGun"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true, true, true, true, true, true, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.addContainerListener(new java.awt.event.ContainerAdapter() {
            public void componentAdded(java.awt.event.ContainerEvent evt) {
                jTable1ComponentAdded(evt);
            }
        });
        jTable1.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                jTable1InputMethodTextChanged(evt);
            }
        });
        jTable1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTable1KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTable1KeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton5.setLabel("Søg");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel1.setText("Søg på produktnavn eller ID");

        jButton1.setText("Tilføj til ordre");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Mængde");

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "OrderID", "Name"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 917, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 1158, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 467, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void displayLatestOrders()
{
    int row = 0;
    for(ModelLager.SalesOrderP ordren : salesOrder.findAllSalesOrders())
    {
        jTable2.setValueAt(ordren.getOrderID(), row, 0);
        //jTable2.setValueAt(ordren.getCustomer().getName(), 0, 0);
        
        row++;
    }
    

}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
    clearTable(jTable1);
    int addRow = 0;
    int addColumn = 0;
    for(ProductP product : products.findProducts(jTextField1.getText()))
    {
        jTable1.setValueAt(product.getProductID(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getName(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getPurchasePrice(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getSalesPrice(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getRentPrice(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getCountryOfOrigin(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getMinStock(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getBarCode(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getSupplierID(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getType(), addRow, addColumn);
        addColumn++;        
        jTable1.setValueAt(product.getDescription(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getSize(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getColor(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getFabric(), addRow, addColumn);
        addColumn++;
        jTable1.setValueAt(product.getDescriptionGun(), addRow, addColumn);
        addRow++;
        addColumn=0;
    }
}
    
    private void clearTable(JTable table)
    {
    int rowCount = 0;
    while(rowCount < table.getRowCount())
    {
        clearColumn(table, rowCount);
        rowCount++;
    }
    }
        private void clearColumn(JTable table, int row)
{
    int column = 0;
    int size = table.getColumnCount();
    while(column < size)
    {
        table.setValueAt(null, row, column);
        column++;
    }


}//GEN-LAST:event_jButton5ActionPerformed

private void jTable1InputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jTable1InputMethodTextChanged

}//GEN-LAST:event_jTable1InputMethodTextChanged

private void jTable1ComponentAdded(java.awt.event.ContainerEvent evt) {//GEN-FIRST:event_jTable1ComponentAdded
}//GEN-LAST:event_jTable1ComponentAdded

private void jTable1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyReleased
// Opdaterer ændringer i tabellen, uploader alle felterne i tabellen selvom
// der kun opdateres 1 information - kan det optimeres?
    try{
        
    int row = jTable1.getSelectedRow();
    String name = "";
    if(jTable1.getValueAt(row, 1) != null)
    {
        name = jTable1.getValueAt(row, 1).toString();
    }
    
    double purchasePrice = 0;
    if(jTable1.getValueAt(row, 2)!=null)
    {
        Double.parseDouble(jTable1.getValueAt(row, 2).toString().trim());
    }
    
    double salesPrice = 0;
    if(jTable1.getValueAt(row, 3)!=null)
    {
        salesPrice = Double.parseDouble(jTable1.getValueAt(row, 3).toString().trim());
    }
    double rentPrice = 0;
    if(jTable1.getValueAt(row, 4) != null)
    {
        rentPrice = Double.parseDouble(jTable1.getValueAt(row, 4).toString().trim());
    }
    
    String countryOfOrigin = "";
    if(jTable1.getValueAt(row, 5)!= null)
    {
        countryOfOrigin = jTable1.getValueAt(row, 5).toString().trim();
    }
    
    int minStock = 0;
    if(jTable1.getValueAt(row, 6)!=null)
    {
        minStock = Integer.parseInt(jTable1.getValueAt(row, 6).toString().trim()); 
    }
    
    int barCode = 0;
    if(jTable1.getValueAt(row, 7) != null)
    {
        barCode = Integer.parseInt(jTable1.getValueAt(row, 7).toString().trim());  
    }
    int productID = 0;
    if(jTable1.getValueAt(row, 0)!=null)
    {
        productID = Integer.parseInt(jTable1.getValueAt(row, 0).toString().trim()); 
    }
    
    int supplierID = 0;
    if(jTable1.getValueAt(row, 8)!=null)
    {
        supplierID = Integer.parseInt(jTable1.getValueAt(row, 8).toString().trim());
    }
    
    String type = "";
    if(jTable1.getValueAt(row, 9) != null )
    {
        type = jTable1.getValueAt(row, 9).toString().trim(); 
    }
    String description = "";
    if(jTable1.getValueAt(row, 10) != null )
    {
        description = jTable1.getValueAt(row, 10).toString().trim(); 
    }   
    
    String size = "";
    if(jTable1.getValueAt(row, 11) != null )
    {
        size = jTable1.getValueAt(row, 11).toString().trim(); 
    }    
    
    String color = "";
    if(jTable1.getValueAt(row, 12) != null )
    {
        color = jTable1.getValueAt(row, 12).toString().trim(); 
    }    
    String fabric = "";
    if(jTable1.getValueAt(row, 13) != null )
    {
        fabric = jTable1.getValueAt(row, 13).toString().trim(); 
    }
    String descriptionGun = "";
    if(jTable1.getValueAt(row, 14) != null )
    {
        descriptionGun = jTable1.getValueAt(row, 14).toString().trim(); 
    }
        
    products.updateProduct(name, purchasePrice, salesPrice, rentPrice, countryOfOrigin, minStock, barCode, productID, supplierID, type, description, size, color, fabric, descriptionGun);
    System.out.println("KeyReleased");
    }catch(Exception e){
        System.out.println("Exception: " + e.getMessage());
    }
}//GEN-LAST:event_jTable1KeyReleased

private void jTable1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTable1KeyTyped
System.out.println("keyTyped");
}//GEN-LAST:event_jTable1KeyTyped

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
            
    int amount = Integer.parseInt(jTextField2.getText().trim());
    
    int pID = Integer.parseInt(jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString().trim());
    int salesId= Integer.parseInt(jTable2.getValueAt(jTable2.getSelectedRow(), 0).toString().trim());
    ProductP product = products.findProduct(pID);
    salesOrder.createPartOrder(amount, salesOrder.findSalesOrder(salesId), product);
    
}//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
